import Vue from 'vue'
import Router from 'vue-router'
// import HmLogin from '@/views/login/index' // 上来就加载

// function fn() {
//   console.log(111)
// }

// fn()

Vue.use(Router)

/* Layout */
// 架子组件 参与参太强了
import Layout from '@/layout' // 直接加载 不用懒加载?
// 8个导入
import approval from './modules/approval'
import department from './modules/department'
import employee from './modules/employee'
import permission from './modules/permission'
import attendance from './modules/attendance'
import salary from './modules/salary'
import role from './modules/role'
import social from './modules/social'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// 8个动态路由( 应该是根据用户的menus字段从中挑选几个塞到路由实例中去)
// 如何给路由实例添加路由规则?   路由实例.addRoutes()
export const asyncRoutes = [
  approval,
  department,
  employee,
  permission,
  attendance,
  salary,
  role,
  social
]
// 静态路由规则
export const constantRoutes = [
  {
    path: '/login',
    // 这个组件只有在路径匹配时才会加载
    // 这个函数会在路径匹配时自动调用
    component: () => import('@/views/login/index'), // 组件懒加载的写法
    hidden: true // 不用展示菜单栏
  },

  // 404
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true // 不用展示菜单栏
  },

  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard', // 重定向
    children: [
      // 子路由
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        // 花哥做了很多
        meta: { title: '主页', icon: 'el-icon-phone' }
      }
      // 子路由标准写法是写在children  花哥考虑到了项目的维护性
      // {
      //   path: '/approval',
      //   component: () => import('@/views/approval/index')
      // }
    ]
  }

  // 真正的逻辑在这里: 不是去控制菜单中的显示隐藏 而是去控制路由规则的有无
  // approval,
  // department,
  // employee,
  // permission,
  // attendance,
  // salary,
  // role,
  // social,

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true } // 不用展示菜单栏
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 用的是写死的静态路由规则 去渲染侧边栏
  routes: constantRoutes // 3个静态路由规则 真正用于显示的就一个
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置 路由
export function resetRouter() {
  const newRouter = createRouter() // 创建新的路由实例
  router.matcher = newRouter.matcher // 将新创建的路由实例核心配置matcher匹配规则 替换到老的里面去  而不是给项目注入一个新的路由实例
}

export default router

// 路由页面分为两种:
// 1. 静态路由: 提前定义好路由页面, 访问时直接跳转 比如登录 首页 404  所有账号都可访问
// 2. 动态路由: 访问时根据条件跳转 比如 审批 组织 员工 权限 考勤 工资 角色 社保 八大页面 不是所有账号默认拥有的  要看你有没有这个权限
